package controller;

import dao.CommodityDao;
import dao.InvedetailsDao;
import dao.InventoryDao;
import entity.Commodity;
import entity.Invedetails;
import entity.Inventory;
import entity.Transfer;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import service.CommodityService;
import service.InvedetailsService;
import util.random1;
import util.random2;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("KCGLPD")
public class KCGLPD {
    @Resource(name = "invedetailsService")
    private InvedetailsService invedetailsService;

    @Resource(name = "invedetailsDao")
    private InvedetailsDao invedetailsDao;

    @Resource(name = "inventoryDao")
    private InventoryDao inventoryDao;

    @Resource(name = "commodityService")
    private CommodityService commodityService;

    @Resource(name = "commodityDao")
    private CommodityDao commodityDao;

    @RequestMapping("add")
    @ResponseBody
    public List<Invedetails> add(Invedetails in){
        Invedetails a = invedetailsDao.queryone(in.getNumber());
        System.out.println("编号："+in.getNumber());
        if(a!=null){
            System.out.println("进来修改");
            return null;
        }else{
            int b = invedetailsService.add(in);
            if(b>0){
                System.out.println("插入成功");
            }
        }
        return invedetailsService.query(1);
    }

    @RequestMapping("update")
    @ResponseBody
    public List<Invedetails> update(Invedetails in){
        int a = invedetailsDao.updateone(in);
        return invedetailsService.query(1);
    }

    @RequestMapping("addorder")
    @ResponseBody
    public List<Inventory> addorder(String order,String date,String name,String remark,int count,int sum){
        Inventory in = new Inventory();
        in.setOrder(order);
        in.setDate(date);
        in.setName(name);
        in.setRemark(remark);
        in.setCount(count);
        in.setSum(sum);
        System.out.println("进来了添加单");
        int a = inventoryDao.add(in);
        int b = invedetailsDao.update(1);
        return inventoryDao.query(1);
    }

    @RequestMapping("queryorder")
    @ResponseBody
    public List<Inventory> queryorder(Inventory in){
//        int a = inventoryDao.add(in);
        return inventoryDao.query(1);
    }

    @RequestMapping("queryorder2")
    @ResponseBody
    public List<Inventory> queryorder2(Inventory in){
//        int a = inventoryDao.add(in);
        return inventoryDao.query(2);
    }

    @RequestMapping("orderxiangxi")
    @ResponseBody
    public List<Invedetails> orderxiangxi(String ordernumber){
//        int a = inventoryDao.add(in);
        System.out.println("单号："+ordernumber);
        return invedetailsService.queryorder(ordernumber);
    }

    @RequestMapping("suiji")
    @ResponseBody
    public String suiji(Model model){
        List<Inventory> list = inventoryDao.query(1);
        random2 r = new random2();
        String fan = r.random(list);
        return fan;
    }

    //按仓库查询
    @RequestMapping("/warequery")
    @ResponseBody
    public Map<String,Object> list(int cangku, int pageNumber, int pageSize){
        System.out.println("进来了分页查询");
        Commodity commodity = new Commodity();
        try {
            commodity.setWarehouseid(cangku);
        }catch (Exception e){
            commodity.setWarehouseid(-1);
        }

        Map<String,Object> map = new HashMap<String,Object>() ;
        Map<String,Object> map1 = new HashMap<String,Object>() ;
        map1.put("warehouseid",cangku);
        map1.put("offset",(pageNumber-1) * pageSize) ;
        map1.put("limit",pageSize) ;


        List<Commodity> list = commodityService.queryByName(map1) ;


        map.put("total",commodityService.count(commodity)) ;
        map.put("rows",list) ;
        return map;
    }
    @RequestMapping("/xiuzheng")
    @ResponseBody
    public List<Inventory> xiuzheng(String bianhao,String shuliang,int cangku,String order){
        String shu = shuliang.substring(1,shuliang.toCharArray().length-1);
        String name = bianhao.substring(1,bianhao.toCharArray().length-1);
        String[] shu1 = shu.split(",");
        String[] name1 = name.split(",");
        int a = 0;
        int b = 0;
        for(int i = 0;i<shu1.length;i++){
            String bian = name1[i].substring(1,name1[i].toCharArray().length-1);
            a = commodityDao.dbupdate(bian,cangku,Integer.parseInt(shu1[i]));
            System.out.println("编号："+bian+"数量："+shu1[i]+"仓库："+cangku+"单号："+order);
        }
        if(a>0){
            b = inventoryDao.update(order);
        }
        if(b>0){
            System.out.println("单号修改状态成功");
        }
        return inventoryDao.query(1);
    }

    @RequestMapping("/querytiaojian")
    @ResponseBody
    public List<Inventory> querytiaojian(String start,String end,String cangku){
        System.out.println("开始时间"+start+"结束时间"+end+"仓库"+cangku);
        return inventoryDao.querytiaojian(start,end,cangku);
    }
}
